package com.example.drug.controller;

import com.example.drug.domain.Member;
import com.example.drug.service.MemberService;
import com.example.drug.util.AjaxResult;
import com.example.drug.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员Controller
 *
 * @author [你的名字]
 * @date [具体日期]
 */
@RestController
@RequestMapping("/api/member")
@Api(tags = "药店会员管理609余函池")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 获取全部会员列表
     */
    @ApiOperation("获取全部会员列表")
    @GetMapping("/memberlist")
    public AjaxResult getAllMemberList() {
        return AjaxResult.success(memberService.selectAllMembers());
    }

    /**
     * 根据姓名模糊查询会员列表
     */
    @ApiOperation("根据姓名模糊查询会员列表")
    @ApiImplicitParam(name = "name", value = "会员姓名",
            dataType = "String", dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String name) {
        List<Member> list = memberService.selectMembersByName(name);
        return AjaxResult.success(list);
    }

    /**
     * 获取会员详细信息
     */
    @ApiOperation("获取会员详细信息")
    @ApiImplicitParam(name = "id", value = "会员ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Member member = memberService.selectMemberById(id);
        if (member == null || member.getS_id() == null) {
            throw new BizException(404, "会员 " + id + " 不存在");
        }
        return AjaxResult.success(member);
    }

    /**
     * 新增会员
     */
    @ApiOperation("新增会员")
    @PostMapping
    public AjaxResult add(@RequestBody Member member) {
        return AjaxResult.success(memberService.insertMember(member));
    }

    /**
     * 修改会员
     */
    @ApiOperation("修改会员")
    @PutMapping
    public AjaxResult edit(@RequestBody Member member) {
        return AjaxResult.success(memberService.updateMember(member));
    }

    /**
     * 删除会员
     */
    @ApiOperation("删除会员")
    @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(memberService.deleteMemberById(id));
    }
}